package com.core.interceptor;

import com.core.controller.IndexController;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

/**
 * 全局拦截器 给jsp页面赋值唯一的timer 和   dialog
 */
public class RenderJspInterceptor  implements Interceptor  {

	@Override
	public void intercept(ActionInvocation me) {
		Controller controller = me.getController();
		String key = me.getActionKey() ;
		String model = "" ;
		if(controller instanceof IndexController){
			int subIndex = key.indexOf("InitView") ;
			if(subIndex != -1)
				model = key.substring(1,subIndex ) ;
		}else{
			String controllerName = controller.getClass().getSimpleName() ;
			model = controllerName.substring(0, controllerName.indexOf("Controller")) ;
			model = StrKit.firstCharToLowerCase(model);
		}
		controller.setAttr("model", model);
		
		controller.setAttr("timer", System.currentTimeMillis());
		if(StrKit.notBlank(controller.getPara(0)))
			controller.setAttr("datagrid", controller.getPara(0));
		if(StrKit.notBlank(controller.getPara(1)))
			controller.setAttr("dialog", controller.getPara(1));
		me.invoke();
	}

}
